#20250613 尤文杰
import tkinter as tk
import random
from tkinter import messagebox

class BinarySearchGame:
    def __init__(self, master):
        self.master = master
        self.master.title("二分查找挑战")
        
        # 生成有序的不连续数字列表
        self.numbers = sorted(random.sample(range(1, 1001), 100))
        self.target = random.choice(self.numbers)
        
        self.labels = []
        
        # 创建方格布局
        for i in range(10):
            row = []
            for j in range(10):
                index = i * 10 + j
                label = tk.Label(master, text="?", width=4, height=2, bg="lightgray", borderwidth=1, relief="solid")
                label.grid(row=i, column=j, padx=5, pady=5)
                label.bind("<Button-1>", lambda event, idx=index: self.show_value(event, idx))
                row.append(label)
            self.labels.append(row)

        # 显示序号
        for i in range(100):
            index_label = tk.Label(master, text=str(i + 1), width=4)
            index_label.grid(row=10, column=i % 10, padx=5, pady=25)

        # 显示目标数字
        self.target_label = tk.Label(master, text=f"目标数字：{self.target}", font=("Arial", 12))
        self.target_label.grid(row=11, columnspan=10)

    def show_value(self, event, index):
        # 先隐藏其他的数值
        for row in self.labels:
            for label in row:
                label.config(text="?")
        
        # 显示当前选择的数字
        self.labels[index // 10][index % 10].config(text=str(self.numbers[index]))
        
        # 检查是否找到目标
        if self.numbers[index] == self.target:
            messagebox.showinfo("查找成功", f"恭喜你，查找成功！目标数字是 {self.target}。")
            self.master.quit()

if __name__ == "__main__":
    root = tk.Tk()
    game = BinarySearchGame(root)
    root.mainloop()